//
//  ybPhotoCollectionViewCell.m
//  OurTourism
//
//  Created by dllo on 15/12/2.
//  Copyright © 2015年 Devil_Coming. All rights reserved.
//

#import "ybPhotoCollectionViewCell.h"
#import "Header.h"

@interface ybPhotoCollectionViewCell () <UIScrollViewDelegate>

@property(nonatomic, strong) UIScrollView *scrollView;
@property(nonatomic, strong) UIImageView *backgroundImg;
@property(nonatomic, strong) UITextView *textView;
@property(nonatomic, strong) UIImageView *timeImg;
@property(nonatomic, strong) UILabel *timeLabel;
@property(nonatomic, strong) UITapGestureRecognizer *tap;

@end



@implementation ybPhotoCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame
{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        self.scrollView = [[UIScrollView alloc] init];
        self.scrollView.delegate = self;
        self.scrollView.backgroundColor = [UIColor blackColor];
        self.scrollView.maximumZoomScale = 2.0;
        self.scrollView.minimumZoomScale = 1.0;
        self.scrollView.bounces = NO;
        self.scrollView.showsVerticalScrollIndicator = NO;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        [self.contentView addSubview:self.scrollView];
        
        self.backgroundImg = [[UIImageView alloc] init];
        self.backgroundImg.contentMode = UIViewContentModeScaleAspectFill;
        self.backgroundImg.userInteractionEnabled = YES;
        [self.scrollView addSubview:self.backgroundImg];
        self.scrollView.contentSize = self.backgroundImg.frame.size;
        
        self.textView = [[UITextView alloc] init];
        self.textView.backgroundColor = [UIColor blackColor];
        self.textView.alpha = .7;
        self.textView.textColor = [UIColor whiteColor];
        self.textView.font = [UIFont fontWithName:@"KohinoorBangla-Regular" size:14];
        self.textView.editable = NO;
        self.textView.textAlignment = NSTextAlignmentJustified;
        [self.contentView addSubview:self.textView];
        
        self.timeImg = [[UIImageView alloc] init];
        self.timeImg.image = [UIImage imageNamed:@"time"];
        [self.contentView addSubview:self.timeImg];
        
        self.timeLabel = [[UILabel alloc] init];
        self.timeLabel.backgroundColor = [UIColor blackColor];
        self.timeLabel.alpha = .7;
        self.timeLabel.textColor = [UIColor whiteColor];
        self.timeLabel.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:self.timeLabel];
        
        self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        self.tap.numberOfTapsRequired = 2;
        [self.backgroundImg addGestureRecognizer:self.tap];
    }
    return self;
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];
    
    self.scrollView.frame = self.contentView.frame;
    self.backgroundImg.frame = CGRectMake(0, layoutAttributes.size.height / 2 / 2, WIDTH, layoutAttributes.size.height / 2);
    self.textView.frame = CGRectMake(10, layoutAttributes.size.height - 70 - 80, WIDTH - 20, 80);
    self.timeImg.frame = CGRectMake(10, layoutAttributes.size.height - 70, 20, 20);
    self.timeLabel.frame = CGRectMake(35, layoutAttributes.size.height - 70, WIDTH - 55, 20);
}

- (void)setTempModel:(YBDestinationModel *)tempModel
{
    
        _tempModel = tempModel;
   
        [self.backgroundImg sd_setImageWithURL:[NSURL URLWithString:tempModel.photo]];
        self.textView.text = tempModel.text;
        self.timeLabel.text = tempModel.local_time;
   
}

// 使用捏合手势时调用
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return _backgroundImg;
}
//完成放大缩小时调用
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
    if (scale == 2) {
        
        scrollView.contentSize = CGSizeMake(view.frame.size.width, view.frame.size.height + 400);
    }
}

- (void)tapAction
{
    
    if (self.scrollView.zoomScale > 1) {
        
        self.scrollView.zoomScale = 1;
        
    }else{
        
        self.scrollView.zoomScale = 2;
    }
}

@end
